/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.caresms.service.persistence;

import com.caresms.NoSuchCountry_MasterException;

import com.caresms.model.Country_Master;
import com.caresms.model.impl.Country_MasterImpl;
import com.caresms.model.impl.Country_MasterModelImpl;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the country_ master service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author cc
 * @see Country_MasterPersistence
 * @see Country_MasterUtil
 * @generated
 */
public class Country_MasterPersistenceImpl extends BasePersistenceImpl<Country_Master>
	implements Country_MasterPersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link Country_MasterUtil} to access the country_ master persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = Country_MasterImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME = new FinderPath(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterModelImpl.FINDER_CACHE_ENABLED,
			Country_MasterImpl.class, FINDER_CLASS_NAME_ENTITY,
			"fetchByFindByCountryName",
			new String[] { String.class.getName() },
			Country_MasterModelImpl.COUNTRYNAME_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_FINDBYCOUNTRYNAME = new FinderPath(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"countByFindByCountryName", new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterModelImpl.FINDER_CACHE_ENABLED,
			Country_MasterImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterModelImpl.FINDER_CACHE_ENABLED,
			Country_MasterImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);

	/**
	 * Caches the country_ master in the entity cache if it is enabled.
	 *
	 * @param country_Master the country_ master
	 */
	public void cacheResult(Country_Master country_Master) {
		EntityCacheUtil.putResult(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterImpl.class, country_Master.getPrimaryKey(),
			country_Master);

		FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
			new Object[] { country_Master.getCountryName() }, country_Master);

		country_Master.resetOriginalValues();
	}

	/**
	 * Caches the country_ masters in the entity cache if it is enabled.
	 *
	 * @param country_Masters the country_ masters
	 */
	public void cacheResult(List<Country_Master> country_Masters) {
		for (Country_Master country_Master : country_Masters) {
			if (EntityCacheUtil.getResult(
						Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
						Country_MasterImpl.class, country_Master.getPrimaryKey()) == null) {
				cacheResult(country_Master);
			}
			else {
				country_Master.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all country_ masters.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(Country_MasterImpl.class.getName());
		}

		EntityCacheUtil.clearCache(Country_MasterImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the country_ master.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(Country_Master country_Master) {
		EntityCacheUtil.removeResult(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterImpl.class, country_Master.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		clearUniqueFindersCache(country_Master);
	}

	@Override
	public void clearCache(List<Country_Master> country_Masters) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (Country_Master country_Master : country_Masters) {
			EntityCacheUtil.removeResult(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
				Country_MasterImpl.class, country_Master.getPrimaryKey());

			clearUniqueFindersCache(country_Master);
		}
	}

	protected void clearUniqueFindersCache(Country_Master country_Master) {
		FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
			new Object[] { country_Master.getCountryName() });
	}

	/**
	 * Creates a new country_ master with the primary key. Does not add the country_ master to the database.
	 *
	 * @param CountryId the primary key for the new country_ master
	 * @return the new country_ master
	 */
	public Country_Master create(long CountryId) {
		Country_Master country_Master = new Country_MasterImpl();

		country_Master.setNew(true);
		country_Master.setPrimaryKey(CountryId);

		return country_Master;
	}

	/**
	 * Removes the country_ master with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param CountryId the primary key of the country_ master
	 * @return the country_ master that was removed
	 * @throws com.caresms.NoSuchCountry_MasterException if a country_ master with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Country_Master remove(long CountryId)
		throws NoSuchCountry_MasterException, SystemException {
		return remove(Long.valueOf(CountryId));
	}

	/**
	 * Removes the country_ master with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the country_ master
	 * @return the country_ master that was removed
	 * @throws com.caresms.NoSuchCountry_MasterException if a country_ master with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public Country_Master remove(Serializable primaryKey)
		throws NoSuchCountry_MasterException, SystemException {
		Session session = null;

		try {
			session = openSession();

			Country_Master country_Master = (Country_Master)session.get(Country_MasterImpl.class,
					primaryKey);

			if (country_Master == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchCountry_MasterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(country_Master);
		}
		catch (NoSuchCountry_MasterException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected Country_Master removeImpl(Country_Master country_Master)
		throws SystemException {
		country_Master = toUnwrappedModel(country_Master);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, country_Master);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		clearCache(country_Master);

		return country_Master;
	}

	@Override
	public Country_Master updateImpl(
		com.caresms.model.Country_Master country_Master, boolean merge)
		throws SystemException {
		country_Master = toUnwrappedModel(country_Master);

		boolean isNew = country_Master.isNew();

		Country_MasterModelImpl country_MasterModelImpl = (Country_MasterModelImpl)country_Master;

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, country_Master, merge);

			country_Master.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew || !Country_MasterModelImpl.COLUMN_BITMASK_ENABLED) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		EntityCacheUtil.putResult(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
			Country_MasterImpl.class, country_Master.getPrimaryKey(),
			country_Master);

		if (isNew) {
			FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
				new Object[] { country_Master.getCountryName() }, country_Master);
		}
		else {
			if ((country_MasterModelImpl.getColumnBitmask() &
					FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						country_MasterModelImpl.getOriginalCountryName()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_FINDBYCOUNTRYNAME,
					args);

				FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
					args);

				FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
					new Object[] { country_Master.getCountryName() },
					country_Master);
			}
		}

		return country_Master;
	}

	protected Country_Master toUnwrappedModel(Country_Master country_Master) {
		if (country_Master instanceof Country_MasterImpl) {
			return country_Master;
		}

		Country_MasterImpl country_MasterImpl = new Country_MasterImpl();

		country_MasterImpl.setNew(country_Master.isNew());
		country_MasterImpl.setPrimaryKey(country_Master.getPrimaryKey());

		country_MasterImpl.setCountryId(country_Master.getCountryId());
		country_MasterImpl.setCountryName(country_Master.getCountryName());
		country_MasterImpl.setField1(country_Master.getField1());
		country_MasterImpl.setField2(country_Master.isField2());
		country_MasterImpl.setField3(country_Master.getField3());
		country_MasterImpl.setField4(country_Master.getField4());
		country_MasterImpl.setField5(country_Master.getField5());

		return country_MasterImpl;
	}

	/**
	 * Returns the country_ master with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the country_ master
	 * @return the country_ master
	 * @throws com.liferay.portal.NoSuchModelException if a country_ master with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public Country_Master findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the country_ master with the primary key or throws a {@link com.caresms.NoSuchCountry_MasterException} if it could not be found.
	 *
	 * @param CountryId the primary key of the country_ master
	 * @return the country_ master
	 * @throws com.caresms.NoSuchCountry_MasterException if a country_ master with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Country_Master findByPrimaryKey(long CountryId)
		throws NoSuchCountry_MasterException, SystemException {
		Country_Master country_Master = fetchByPrimaryKey(CountryId);

		if (country_Master == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + CountryId);
			}

			throw new NoSuchCountry_MasterException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				CountryId);
		}

		return country_Master;
	}

	/**
	 * Returns the country_ master with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the country_ master
	 * @return the country_ master, or <code>null</code> if a country_ master with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public Country_Master fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the country_ master with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param CountryId the primary key of the country_ master
	 * @return the country_ master, or <code>null</code> if a country_ master with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Country_Master fetchByPrimaryKey(long CountryId)
		throws SystemException {
		Country_Master country_Master = (Country_Master)EntityCacheUtil.getResult(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
				Country_MasterImpl.class, CountryId);

		if (country_Master == _nullCountry_Master) {
			return null;
		}

		if (country_Master == null) {
			Session session = null;

			boolean hasException = false;

			try {
				session = openSession();

				country_Master = (Country_Master)session.get(Country_MasterImpl.class,
						Long.valueOf(CountryId));
			}
			catch (Exception e) {
				hasException = true;

				throw processException(e);
			}
			finally {
				if (country_Master != null) {
					cacheResult(country_Master);
				}
				else if (!hasException) {
					EntityCacheUtil.putResult(Country_MasterModelImpl.ENTITY_CACHE_ENABLED,
						Country_MasterImpl.class, CountryId, _nullCountry_Master);
				}

				closeSession(session);
			}
		}

		return country_Master;
	}

	/**
	 * Returns the country_ master where CountryName = &#63; or throws a {@link com.caresms.NoSuchCountry_MasterException} if it could not be found.
	 *
	 * @param CountryName the country name
	 * @return the matching country_ master
	 * @throws com.caresms.NoSuchCountry_MasterException if a matching country_ master could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Country_Master findByFindByCountryName(String CountryName)
		throws NoSuchCountry_MasterException, SystemException {
		Country_Master country_Master = fetchByFindByCountryName(CountryName);

		if (country_Master == null) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("CountryName=");
			msg.append(CountryName);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			if (_log.isWarnEnabled()) {
				_log.warn(msg.toString());
			}

			throw new NoSuchCountry_MasterException(msg.toString());
		}

		return country_Master;
	}

	/**
	 * Returns the country_ master where CountryName = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
	 *
	 * @param CountryName the country name
	 * @return the matching country_ master, or <code>null</code> if a matching country_ master could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Country_Master fetchByFindByCountryName(String CountryName)
		throws SystemException {
		return fetchByFindByCountryName(CountryName, true);
	}

	/**
	 * Returns the country_ master where CountryName = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
	 *
	 * @param CountryName the country name
	 * @param retrieveFromCache whether to use the finder cache
	 * @return the matching country_ master, or <code>null</code> if a matching country_ master could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Country_Master fetchByFindByCountryName(String CountryName,
		boolean retrieveFromCache) throws SystemException {
		Object[] finderArgs = new Object[] { CountryName };

		Object result = null;

		if (retrieveFromCache) {
			result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
					finderArgs, this);
		}

		if (result instanceof Country_Master) {
			Country_Master country_Master = (Country_Master)result;

			if (!Validator.equals(CountryName, country_Master.getCountryName())) {
				result = null;
			}
		}

		if (result == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_SELECT_COUNTRY_MASTER_WHERE);

			if (CountryName == null) {
				query.append(_FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_1);
			}
			else {
				if (CountryName.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_3);
				}
				else {
					query.append(_FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (CountryName != null) {
					qPos.add(CountryName);
				}

				List<Country_Master> list = q.list();

				result = list;

				Country_Master country_Master = null;

				if (list.isEmpty()) {
					FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
						finderArgs, list);
				}
				else {
					country_Master = list.get(0);

					cacheResult(country_Master);

					if ((country_Master.getCountryName() == null) ||
							!country_Master.getCountryName().equals(CountryName)) {
						FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
							finderArgs, country_Master);
					}
				}

				return country_Master;
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (result == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_FINDBYCOUNTRYNAME,
						finderArgs);
				}

				closeSession(session);
			}
		}
		else {
			if (result instanceof List<?>) {
				return null;
			}
			else {
				return (Country_Master)result;
			}
		}
	}

	/**
	 * Returns all the country_ masters.
	 *
	 * @return the country_ masters
	 * @throws SystemException if a system exception occurred
	 */
	public List<Country_Master> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the country_ masters.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of country_ masters
	 * @param end the upper bound of the range of country_ masters (not inclusive)
	 * @return the range of country_ masters
	 * @throws SystemException if a system exception occurred
	 */
	public List<Country_Master> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the country_ masters.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of country_ masters
	 * @param end the upper bound of the range of country_ masters (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of country_ masters
	 * @throws SystemException if a system exception occurred
	 */
	public List<Country_Master> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = new Object[] { start, end, orderByComparator };

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<Country_Master> list = (List<Country_Master>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_COUNTRY_MASTER);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_COUNTRY_MASTER;
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<Country_Master>)QueryUtil.list(q,
							getDialect(), start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<Country_Master>)QueryUtil.list(q,
							getDialect(), start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes the country_ master where CountryName = &#63; from the database.
	 *
	 * @param CountryName the country name
	 * @return the country_ master that was removed
	 * @throws SystemException if a system exception occurred
	 */
	public Country_Master removeByFindByCountryName(String CountryName)
		throws NoSuchCountry_MasterException, SystemException {
		Country_Master country_Master = findByFindByCountryName(CountryName);

		return remove(country_Master);
	}

	/**
	 * Removes all the country_ masters from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (Country_Master country_Master : findAll()) {
			remove(country_Master);
		}
	}

	/**
	 * Returns the number of country_ masters where CountryName = &#63;.
	 *
	 * @param CountryName the country name
	 * @return the number of matching country_ masters
	 * @throws SystemException if a system exception occurred
	 */
	public int countByFindByCountryName(String CountryName)
		throws SystemException {
		Object[] finderArgs = new Object[] { CountryName };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_FINDBYCOUNTRYNAME,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_COUNTRY_MASTER_WHERE);

			if (CountryName == null) {
				query.append(_FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_1);
			}
			else {
				if (CountryName.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_3);
				}
				else {
					query.append(_FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (CountryName != null) {
					qPos.add(CountryName);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_FINDBYCOUNTRYNAME,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of country_ masters.
	 *
	 * @return the number of country_ masters
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_COUNTRY_MASTER);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the country_ master persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.caresms.model.Country_Master")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<Country_Master>> listenersList = new ArrayList<ModelListener<Country_Master>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<Country_Master>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(Country_MasterImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@BeanReference(type = Category_MasterPersistence.class)
	protected Category_MasterPersistence category_MasterPersistence;
	@BeanReference(type = City_MasterPersistence.class)
	protected City_MasterPersistence city_MasterPersistence;
	@BeanReference(type = Country_MasterPersistence.class)
	protected Country_MasterPersistence country_MasterPersistence;
	@BeanReference(type = Keyword_MasterPersistence.class)
	protected Keyword_MasterPersistence keyword_MasterPersistence;
	@BeanReference(type = Keyword_Sms_RelationPersistence.class)
	protected Keyword_Sms_RelationPersistence keyword_Sms_RelationPersistence;
	@BeanReference(type = Sms_MasterPersistence.class)
	protected Sms_MasterPersistence sms_MasterPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_COUNTRY_MASTER = "SELECT country_Master FROM Country_Master country_Master";
	private static final String _SQL_SELECT_COUNTRY_MASTER_WHERE = "SELECT country_Master FROM Country_Master country_Master WHERE ";
	private static final String _SQL_COUNT_COUNTRY_MASTER = "SELECT COUNT(country_Master) FROM Country_Master country_Master";
	private static final String _SQL_COUNT_COUNTRY_MASTER_WHERE = "SELECT COUNT(country_Master) FROM Country_Master country_Master WHERE ";
	private static final String _FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_1 = "country_Master.CountryName IS NULL";
	private static final String _FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_2 = "country_Master.CountryName = ?";
	private static final String _FINDER_COLUMN_FINDBYCOUNTRYNAME_COUNTRYNAME_3 = "(country_Master.CountryName IS NULL OR country_Master.CountryName = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "country_Master.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Country_Master exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Country_Master exists with the key {";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(Country_MasterPersistenceImpl.class);
	private static Country_Master _nullCountry_Master = new Country_MasterImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<Country_Master> toCacheModel() {
				return _nullCountry_MasterCacheModel;
			}
		};

	private static CacheModel<Country_Master> _nullCountry_MasterCacheModel = new CacheModel<Country_Master>() {
			public Country_Master toEntityModel() {
				return _nullCountry_Master;
			}
		};
}